package com.googlecode.linkmein.domain;

import java.util.ArrayList;

public class Employees extends ArrayList<Employee> {
    public Employees(Employee... employees) {
        if (employees == null) {
            return;
        }
        for (Employee employee : employees) {
            this.add(employee);
        }
    }

    public static Employees get(Contacts contacts, Resources resources, Gossipies gossipies, Albums albums, MapInfoes mapInfoes) {
        Employees employees = new Employees();
        for (Contact contact : contacts) {
            Resource resource = resources.findResource(contact);
            Employee employee = new Employee(contact, resource);
            gossipies.loadGossipy(employee);
            albums.loadAlbum(employee);
            mapInfoes.LoadMapInfo(employee);
            employees.add(employee);
        }
        return employees;
    }

    public Employee findByAnyIMId(InstantMessenger... ims) {
        ArrayList<InstantMessenger> notNullIms = new ArrayList<InstantMessenger>();
        for (InstantMessenger im : ims) {
            if (im != null) {
                notNullIms.add(im);
            }
        }
        Employee employee = Employee.NULL;
        for (InstantMessenger im : notNullIms) {
            employee = im.find(this);
            if (!(employee instanceof NullEmployee)) {
                break;
            }
        }
        for (InstantMessenger im : notNullIms) {
            im.initialize(employee);
        }
        return employee;
    }

    public Employee findByGmail(String gmail) {
        for (Employee employee : this) {
            if (employee.getGmail().equals(gmail)) {
                return employee;
            }
        }
        return Employee.NULL;
    }

    public Employee findByName(Name name) {
        for (Employee employee : this) {
            if (employee.hasName(name)) {
                return employee;
            }
        }
        return Employee.NULL;
    }

    public Employee findByYahooId(String yahooId) {
        for (Employee employee : this) {
            if (employee.getYIM().equals(yahooId)) {
                return employee;
            }
        }
        return Employee.NULL;
    }

    public Employee findByMsnId(String msnId) {
        for (Employee employee : this) {
            if (employee.getMSN().equals(msnId)) {
                return employee;
            }
        }
        return Employee.NULL;
    }

    public Employee getPrevEmployee(Employee employee) {
        return this.get((this.indexOf(employee) - 1 + this.size()) % this.size());
    }

    public Employee getNextEmployee(Employee employee) {
        return this.get((this.indexOf(employee) + 1) % this.size());
    }
}
